﻿using NoteSystem.utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NoteSystem
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitNote();
        }
        public void InitNote()
        {
            DataSet ds = DbHelper.Query("select n.*,u.account from notes as n inner join users as u on n.user_id=u.id ");
            rptNote.DataSource = ds;
            rptNote.DataBind();
        }
        public string InitUser()
        {
            if (Session["User"] == null)
                Response.Redirect("Login.aspx");
            DataSet userDS = (DataSet)Session["User"];
            StringBuilder str = new StringBuilder();
            str.Append("<div class=\"segment left\">");
            str.Append(" <i class=\"glyphicon glyphicon-user left\"></i>");
            str.Append($"   <h3 class=\"left\">欢迎{userDS.Tables[0].Rows[0]["account"]}登录<br>");
            str.Append("   <span>管理这个设备及其传感器</span>");
            str.Append("  </h3>");
            str.Append(" </div>");
            str.Append(" <div class=\"clear\"></div>");
            str.Append("   <div class=\"segment2\">");
            str.Append(" <div class=\"user_left\">");
            str.Append($"    <img src=\"{userDS.Tables[0].Rows[0]["img_url"]}\">");
            str.Append("   </div>");
            str.Append("  <div class=\"user_right\">");
            str.Append($"  <h5>你好：{userDS.Tables[0].Rows[0]["account"]}<br>");
            str.Append($" <span><i class=\"glyphicon glyphicon-time\"></i>&nbsp;{userDS.Tables[0].Rows[0]["last_login_time"]}</span>");
            str.Append("  </h5>");
            str.Append("    <p>");
            str.Append($"     所在地:{userDS.Tables[0].Rows[0]["city_name"]}<br>");
            string gender = userDS.Tables[0].Rows[0]["gender"].ToString() == "0" ? "男" : "女";
            str.Append($"     性别:{gender}<br>");
            str.Append($"      QQ:{userDS.Tables[0].Rows[0]["qq"]}<br>");
            str.Append($"    邮箱：{userDS.Tables[0].Rows[0]["email"]}@qq.com");
            str.Append("   </p>");
            str.Append("  </div>  ");
            str.Append("</div>");
            return str.ToString();
        }

        protected void rptNote_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void rptNote_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //逐行绑定事件
            //1.拿到当前行的发布人id=>拿当前行隐藏控件的值
            int noteUserId = Convert.ToInt32((e.Item.FindControl("hfUserId") as HiddenField).Value);
            //2.登录人的id
            DataSet dsUser = (DataSet)Session["User"];
            int loginUserId = (int)dsUser.Tables[0].Rows[0]["id"];
            //3.登录人的权限
            int manager = (int)dsUser.Tables[0].Rows[0]["manager"];
            //4.根据你的业务进行判断
            if (manager != 1 && loginUserId != noteUserId)
            {
                //既不是管理员也不是日志发布人
                e.Item.FindControl("LinkButton1").Visible = false;

            }
        }
    }
}